package com.bdqn.ssm.config;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

/**
 * @Author 27435
 * @Date 2025/1/215:44
 * @Version 1.0
 */

@Configuration
@ComponentScan({"com.bdqn.ssm.controller", "com.bdqn.ssm.service", "com.bdqn.ssm.config"})
@Import(JDBCConfig.class)
@EnableWebMvc
public class SpringConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.bdqn.ssm.pojo");
        // 配置日志
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(StdOutImpl.class);
        configuration.setCacheEnabled(true); // 开启缓存
        configuration.setMapUnderscoreToCamelCase(true); // 开启驼峰名
        factoryBean.setConfiguration(configuration);
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.bdqn.ssm.mapper");
        return msc;
    }

}
